package game.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.config.exception.CustomException;
import common.controller.base.BaseController;
import common.model.enums.ClassificationTypeEnum;
import common.model.po.GItem;
import common.model.request.base.PaginationRequest;
import common.model.response.base.Result;
import game.model.request.SaveItemRequest;
import game.model.request.SearchItemRequest;
import game.model.vo.ItemDetailVo;
import game.model.vo.SearchItemVo;
import game.service.GClassificationService;
import game.service.GItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 物品（包括零件和设备两类）前端控制器
 *
 * @author 米泽鹏
 * @since 2022-02-26
 */
@Api(tags = "物品管理")
@RestController
@RequestMapping("/game/g-item")
public class GItemController extends BaseController {

	@Resource
	private GItemService itemService;
	@Resource
	private GClassificationService classificationService;

	@ApiOperation(value = "物品分页查询", notes = "物品分页查询")
	@PostMapping("/readOnly/searchItemByPage")
	public Result<Page<SearchItemVo>> searchItemByPage(@ApiParam(value = "物品分页查询请求", required = true) @RequestBody @Validated PaginationRequest<SearchItemRequest, GItem> paginationRequest) {
		return successWithData(itemService.searchItemByPage(paginationRequest));
	}

	@ApiOperation(value = "单个物品查询", notes = "单个物品查询")
	@GetMapping("/readOnly/getItemDetailById/{itemId}")
	public Result<ItemDetailVo> getItemById(@ApiParam(value = "物品id", required = true) @PathVariable Long itemId) {
		return successWithData(itemService.getItemDetailById(itemId));
	}

	@ApiOperation(value = "物品新增或更新", notes = "物品新增或更新")
	@PostMapping("/saveItem")
	public Result<?> saveItem(@ApiParam(value = "物品新增或更新请求", required = true) @RequestBody @Validated SaveItemRequest request) {
		if (request.getEquippable() == 1 && request.getEquipmentLocation() == null) {
			throw new CustomException("装备位置不能为空");
		}
		if (request.getEquippable() == 0) {
			request.setEquipmentLocation(0);
		}
		return itemService.saveItem(request);
	}

	@ApiOperation(value = "物品删除", notes = "物品删除")
	@DeleteMapping("/deleteItem/{id}")
	public Result<?> deleteItem(@ApiParam(value = "物品id", required = true) @PathVariable Long id) {
		return itemService.deleteItem(id);
	}

	@ApiOperation(value = "获取全部分类（级联选择器）", notes = "获取全部分类（级联选择器）")
	@PostMapping("/readOnly/getItemCascadeClassification")
	public Result<?> getItemCascadeClassification() {
		return successWithData(classificationService.getCascadeClassification(ClassificationTypeEnum.ITEM));
	}

}
